<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.originalFileName" clearable placeholder="原文件名" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.appType" clearable filterable
                   placeholder="文件类型" style="width: 185px;" class="filter-item" @change="crud.toQuery" >
          <el-option
                  v-for="item in dict.app_type"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value" />
        </el-select>
        <el-input v-model="query.suffix" clearable placeholder="后缀" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" >
        <div slot = 'left' style="display: inline-block">
          <el-upload
            :action="baseApi + '/api/deviceFile/upload'"
            :data="uploadData"
            :headers="headers"
            name="file"
            multiple
            :show-file-list="false"
            :auto-upload="true"
            :on-error="handleError"
            :on-success="handleSuccess"
            :before-upload="handleBeforeUpload"
          >
            <el-button
              v-permission="permission.add"
              class="filter-item"
              size="mini"
              type="primary"
              icon="el-icon-upload2"
            >
              上传文件
            </el-button>
          </el-upload>
        </div>
      </crudOperation>

      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="originalFileName" label="原文件名" width="250px" align="center" />
        <el-table-column prop="fileName" label="系统文件名" width="300px" align="center"/>
        <el-table-column prop="appType" label="应用类型"  width="100px" align="center">
          <template slot-scope="scope">
            {{ dict.label.app_type[scope.row.appType] }}
          </template>
        </el-table-column>
        <el-table-column prop="suffix" label="后缀" width="100px" align="center"/>
        <el-table-column prop="diskPath" label="磁盘路径" width="450px" align="center"/>
        <el-table-column prop="visitPath" label="访问路径" width="450px" align="center"/>
        <el-table-column prop="createBy" label="创建人" width="100px" align="center"/>
        <el-table-column prop="createTime" label="创建时间" width="150px" align="center"/>
        <el-table-column prop="updateBy" label="更新人" width="100px" align="center"/>
        <el-table-column prop="updateTime" label="更新时间" width="150px" align="center"/>
        <el-table-column v-if="checkPer(['admin','deviceFile:edit','deviceFile:del'])" fixed="right" label="操作" width="200px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            >
              <el-button slot="view"   size="mini" type="success" icon="el-icon-download" @click="downloadFile(scope.row)" />
            </udOperation>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDeviceFile from '@/api/device/deviceFile'
import { getToken } from '@/utils/auth'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'

const defaultForm = { id: null, deviceId: null, diskPath: null, visitPath: null, originalFileName: null, fileName: null, appType: null, suffix: null, createBy: null, createTime: null, updateBy: null, updateTime: null }
export default {
  name: 'DeviceFile',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['app_type'],
  props: ['deviceId'],
  computed: {
  ...mapGetters([
      'baseApi'
    ])
  },
  cruds() {
    return CRUD({
      title: '设备文件',
      url: 'api/deviceFile',
      idField: 'id',
      sort: 'id,desc',
      optShow: {
        add: false,
        edit: false,
        del: true,
        download: false,
        reset: true,
        import: false
      },
      queryOnPresenterCreated: false,
      crudMethod: { ...crudDeviceFile }}
    )
  },
  data() {
    return {
      uploadData:{
        deviceId: this.deviceId
      },
      headers: {
        'Authorization': getToken()
      },
      permission: {
        add: ['admin', 'deviceFile:add'],
        edit: ['admin', 'deviceFile:edit'],
        del: ['admin', 'deviceFile:del']
      },
      rules: {
        diskPath: [
          { required: true, message: '磁盘路径不能为空', trigger: 'blur' }
        ],
        visitPath: [
          { required: true, message: '访问路径不能为空', trigger: 'blur' }
        ],
        originalFileName: [
          { required: true, message: '原文件名不能为空', trigger: 'blur' }
        ],
        fileName: [
          { required: true, message: '文件名不能为空', trigger: 'blur' }
        ],
        appType: [
          { required: true, message: '应用类型不能为空', trigger: 'blur' }
        ],
        suffix: [
          { required: true, message: '后缀不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: 'ID' },
        { key: 'deviceId', display_name: '设备id' },
        { key: 'diskPath', display_name: '磁盘路径' },
        { key: 'visitPath', display_name: '访问路径' },
        { key: 'originalFileName', display_name: '原文件名' },
        { key: 'fileName', display_name: '文件名' },
        { key: 'appType', display_name: '应用类型' },
        { key: 'suffix', display_name: '后缀' },
        { key: 'createBy', display_name: '创建人' },
        { key: 'updateBy', display_name: '更新人' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.params.deviceId = this.deviceId;
      return true
    },
    //上传成功
    handleSuccess(response, file, fileList){
      //关闭遮罩
      this.crud.loading = false;
      //提示
      this.$message.success("上传成功！");
      //刷新
      this.crud.toQuery();
    },
    //上传失败
    handleError(err, file, fileList){
      //关闭遮罩
      this.crud.loading = false;
      //提示
      this.$message.error(err.message);
    },
    //上传之前
    handleBeforeUpload(file){
      //打开遮罩
      this.crud.loading = true;

    },
    //下载
    downloadFile(data){
      let a = document.createElement("a"); //创建一个a元素
      a.href = this.baseApi +'/' + data.visitPath;    //a元素图片地址
      a.download = data.originalFileName; //图片名
      a.click();
    }
  },
  mounted() {
    this.crud.toQuery();
  }
}
</script>

<style scoped>

</style>
